package com.syezon.wifikey.safe.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.syezon.wifikey.R;
import com.syezon.wifikey.safe.SafeCheckItemInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SafeCheckAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<SafeCheckItemInfo> mData = new ArrayList<>();

    private CheckItemClickListener mListener;

    public SafeCheckAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<SafeCheckItemInfo> data) {
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void addData(SafeCheckItemInfo safeCheckItemInfo) {
        if (safeCheckItemInfo != null) {
            mData.add(safeCheckItemInfo);
            notifyItemInserted(mData.size() - 1);
        }
    }

    public void setListener(CheckItemClickListener listener) {
        this.mListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == SafeCheckItemInfo.TYPE_TITLE) {
            return new CheckTitleViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_check_title, parent, false));
        }
        if (viewType == SafeCheckItemInfo.TYPE_CHECK_ITEM) {
            return new CheckItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_check, parent, false));
        }
        if (viewType == SafeCheckItemInfo.TYPE_ENHANCE) {
            return new CheckItemEnhanceViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_check_enhance, parent, false));
        }
        if (viewType == SafeCheckItemInfo.TYPE_DIVIDER) {
            return new CheckDividerViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_check_divider, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder != null) {
            SafeCheckItemInfo safeCheckItemInfo = mData.get(position);
            int type = safeCheckItemInfo.getType();
            if (type == SafeCheckItemInfo.TYPE_TITLE) {
                CheckTitleViewHolder viewHolder = (CheckTitleViewHolder) holder;
                viewHolder.tvTitle.setText(safeCheckItemInfo.getTitle());
                if (safeCheckItemInfo.isFinish()) {
                    viewHolder.ivProgress.setVisibility(View.GONE);
                    viewHolder.tvItemCount.setVisibility(View.VISIBLE);
                    viewHolder.tvItemCount.setText(safeCheckItemInfo.getTotal() + "条");
                } else {
                    viewHolder.ivProgress.setVisibility(View.VISIBLE);
                    viewHolder.tvItemCount.setVisibility(View.GONE);
                    Animation animation = viewHolder.ivProgress.getAnimation();
                    if (animation == null) {
                        viewHolder.ivProgress.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.rotate));
                    }
                    if (animation != null && !animation.hasStarted()) {
                        viewHolder.ivProgress.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.rotate));
                    }
                }
            }
            if (type == SafeCheckItemInfo.TYPE_CHECK_ITEM) {
                CheckItemViewHolder viewHolder = (CheckItemViewHolder) holder;
                viewHolder.tvTitle.setText(safeCheckItemInfo.getTitle());
                if (safeCheckItemInfo.isFinish()) {
                    viewHolder.llCheckSafe.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.llCheckSafe.setVisibility(View.GONE);
                }
            }
            if (type == SafeCheckItemInfo.TYPE_ENHANCE) {
                CheckItemEnhanceViewHolder viewHolder = (CheckItemEnhanceViewHolder) holder;
                viewHolder.tvTitle.setText(safeCheckItemInfo.getTitle());
                viewHolder.tvEnhance.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mListener != null) {
                            mListener.onEnhance();
                        }
                    }
                });
            }
            if (type == SafeCheckItemInfo.TYPE_DIVIDER) {
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        SafeCheckItemInfo itemInfo = mData.get(position);
        return itemInfo.getType();
    }

    public static class CheckDividerViewHolder extends RecyclerView.ViewHolder {

        public CheckDividerViewHolder(View itemView) {
            super(itemView);
        }
    }

    public static class CheckTitleViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.iv_progress)
        ImageView ivProgress;
        @BindView(R.id.tv_item_count)
        TextView tvItemCount;

        public CheckTitleViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class CheckItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.ll_check_state)
        LinearLayout llCheckSafe;

        public CheckItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class CheckItemEnhanceViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_enhance)
        TextView tvEnhance;

        public CheckItemEnhanceViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface CheckItemClickListener {
        void onEnhance();
    }
}
